package cn.dansj.common.utils.lock;

import cn.dansj.common.utils.bean.DBConnection;
import cn.dansj.common.utils.transfer.Verification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractLock implements Lock {
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 锁名称,必传,区分不同应用
     */
    protected final String lockName;
    /**
     * 等待获取锁的超时时间,超过这个时间将不在等待,报异常退出(ms),0立即退出不等待
     */
    protected final long tryGetLockWaitMaxMs;
    /**
     * 锁的有效期,超过这个时间,锁将失效,后面的线程将可以重新持有该锁(ms),0不失效
     */
    protected final long lockExpireTimeMs;

    /**
     *
     * @param lockName 锁名称
     * @param tryGetLockWaitMaxMs 获取锁最大等待时间 0不等待
     * @param lockExpireTimeMs 锁超时时间 0不超时
     * @param autoLock 自动加锁，自行实现在构造函数中执行lock
     * @param dbConnection 连接信息，备用补充
     * @throws LockException 接收lock方法抛出的异常
     */
    public AbstractLock(String lockName, long tryGetLockWaitMaxMs, long lockExpireTimeMs, boolean autoLock, DBConnection dbConnection) throws LockException {
        //锁名称不能为空
        if (Verification.checkNull(lockName)) {
            throw new IllegalArgumentException("lock name can't empty");
        }
        this.lockName = lockName;
        this.tryGetLockWaitMaxMs = tryGetLockWaitMaxMs;
        this.lockExpireTimeMs = lockExpireTimeMs;
    }
}
